<?php
/**
 * JobsManager - Main Object managing all job Object created in application
 * 
 * @author PoLuX
 */

// Job Class including ...
include_file('src/02_class/LogManager.class.php');
include_file('src/02_class/jobs/AutoDL.job_class.php');
include_file('src/02_class/jobs/AutoAspireVideo.job_class.php');

/** 
 * JobsManager Class - Jobs management class
 * 
 * @final 
 * @author polux
 * 
 */
final class JobsManager {
	
	// ************************************ Static properties ************************************ //
	/**
	 * LogsManager Object 
	
	 * @var LogManager
	 * @access protected
	 * @staticvar
	 */
	protected static $_o_logManager = null;	
	
	/**
	 * Jobs Object table
	 * 
	 * @var array(AJob)
	 * @access protected
	 * @staticvar
	 */
	protected static $_ao_jobsObjects = array();	
		
	// ************************************ Static methods ************************************ //
	
	// CLASS MANAGEMENT ----------------------------------------------------------------------  //
	/**
	 * Initialization of Job Manager
	 *
	 * @static
	 * @staticvar JobsManager::$_ao_jobsObjects
	 */
	public static function initializeManager()
	{
		JobsManager::$_ao_jobsObjects = array();
		JobsManager::$_o_logManager = new LogManager();
		JobsManager::$_o_logManager->setOutputStream("STDOUT", 'php://stdout');
	}
	
	// LOG MANAGEMENT ------------------------------------------------------------------------  //
	/**
	 * addLogJobEvent - Add an event about a Job object
	 * 
	 * @param 	AJob 		$p_o_obj	Job Object
	 * @param 	string 		$p_s_msg	Message
	 * @deprecated
	 */
	public static function addLogJobEvent(AJob $p_o_obj,$p_s_msg)
	{
		JobsManager::$_o_logManager->addLogInformation($p_o_obj, $p_s_msg);
	}
	
	/**
	 * getLogManager
	 * 
	 * @return LogManager
	 */
	public static function getLogManager()
	{
		return JobsManager::$_o_logManager;
	}
		
	// JOBS MANAGEMENT -----------------------------------------------------------------------  //
	/**
	 * registerJob - Add a job to collection
	 *
	 * @param AJob $p_o_job
	 */
	protected static function registerJob(AJob $p_o_job)
	{
		JobsManager::$_ao_jobsObjects[] = &$p_o_job;
	}
	/**
	 * loadAndRunJob -  Load and try to run job
	 *
	 * @param string $p_s_jobName	Job Name
	 * @throws JobNotFoundException
	 * @static
	 */
	public static function loadAndRunJob($p_s_jobName)
	{
		$l_o_job = JobsManager::loadJob($p_s_jobName);
		JobsManager::runJob($l_o_job);
	}
	/**
	 * Return a Job Object
	 *
	 * @param 	string 	$p_s_jobName	Job Name
	 * @return 	AJob	Job Object
	 */
	private static function loadJob($p_s_jobName)
	{
		if(php_jobs_launcher::isJobPropertiesFileExists($p_s_jobName))
		{
			// Getting classname from type
			$l_s_className = php_jobs_launcher::identifyTypeOfJobFromJobName($p_s_jobName);
			$l_s_className = $l_s_className.JOB_INTERNAL_SUFFIXE_JOBCLASSNAME;

			// Job instanciation
			$l_o_job= new $l_s_className($p_s_jobName,php_jobs_launcher::getDefaultFilePathForAJob($p_s_jobName));
					
			// Check real type of object
			if($l_o_job instanceof AJob)
			{
				JobsManager::getLogManager()->addLogSubTitle("Initializing Job '".$l_s_className."'...");
				// Register
				JobsManager::registerJob($l_o_job);
				$l_o_job->setInternalID($l_s_className."_".$p_s_jobName);
				$l_o_job->loadPropertiesFromIniFile();
			}
	
			return $l_o_job;
		}
		else
			throw new JobNotFoundException($p_s_jobName);
	}
	/**
	 * runJob - Run Job Object
	 *
	 * @param 	AJob 	$p_o_jobObj		Job object
	 */
	private static function runJob(AJob $p_o_jobObj)
	{
		try{
			if($p_o_jobObj instanceof IPreProcessedJob)
			{
				$p_o_jobObj->PreProcessJob();
			}
			if($p_o_jobObj instanceof IRunnableJob)
			{
				$p_o_jobObj->RunJob();
			}
// 			if($p_o_jobObj instanceof IPostProcessedJob)
// 			{
// 				$p_o_jobObj->PostProcessJob();
// 			}
		}
		catch(Exception $e)
		{
			ob_start();
			JobsManager::getLogManager()->flushAllLogsOnAllStream();
			ob_flush();
			echo $e->getMessage()."\n";
		}
	}


}

?>